package com.xingkeduo.controller.cms.marketing;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.xingkeduo.controller.base.InitController;
import com.xingkeduo.model.param.marketing.MarketingReleaseHistoryParam;
import com.xingkeduo.service.cms.marketing.MarketingReleaseService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: MarketingReleaseCmsController
 * @Description: 客服服务，券发放记录
 * @author lixin
 * @date May 24, 2019 5:58:10 PM
 *
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/release")
public class MarketingReleaseCmsController extends InitController {

	@Autowired
	MarketingReleaseService marketingReleaseService;

	@RequestMapping("/getCustormerServiceReleaseList")
	public ResponseMessage getCustormerServiceReleaseList(MarketingReleaseHistoryParam releaseTemplateExample) {
		return marketingReleaseService.getCustormerServiceReleaseList(releaseTemplateExample);
	}

	@RequestMapping("/addMarketingRelease")
	public ResponseMessage addMarketingRelease(@RequestBody MarketingReleaseHistoryParam releaseTemplateExample) {
		if (StringUtils.isBlank(releaseTemplateExample.getReleaseData())) {
			return ResponseMessage.error("发放券，卡信息为空", 500);
		}
		JSONArray releaseArray = JSONArray.parseArray(releaseTemplateExample.getReleaseData());
		if(releaseArray==null || releaseArray.isEmpty()) {
			return ResponseMessage.error("发放券，卡信息为空", 500);
		}
		this.initUserInParams(releaseTemplateExample);
		try {
			return marketingReleaseService.addMarketingRelease(releaseTemplateExample);
		} catch (Exception e) {
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/getCustormerServiceReleaseDetail")
	public ResponseMessage getCustormerServiceReleaseDetail(Long marketingReleaseHistoryId) {
		return marketingReleaseService.getCustormerServiceReleaseDetail(marketingReleaseHistoryId);
	}
}
